using System;
using System.ComponentModel;
using System.Diagnostics;

namespace BassMati.Utilities
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class Event
	{
		private Event ( ) { }

		/// <summary>
		/// Marshalls the event handler call to the thread that created the event handler's object.
		/// </summary>
		/// <param name="theEvent">The event to be fired.</param>
		/// <param name="args">The event arguments.</param>
		/// <remarks>
		/// If the event handler is a method of a control, the call will be marshalled to the GUI thread, if required.
		/// </remarks>
		/// 
		public static void Fire ( Delegate theEvent, object [ ] args )
		{
			if ( theEvent == null ) return;
			try
			{
				foreach ( Delegate d  in theEvent.GetInvocationList ( ))
				{
					ISynchronizeInvoke target = d.Target as ISynchronizeInvoke;
					if ( target == null || ! target.InvokeRequired ) d.DynamicInvoke( args );
					else target.Invoke( d, args ); 
				}
			}
			catch ( Exception ex )
			{
				Trace.WriteLine( "FireEvent: " + ex.Message );
			}
		}
	}
}
